﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.Entity;
using MMPS.Entity.POCOMdel;
using System.Runtime.Remoting.Messaging;

namespace MMPS.Business
{
    /// <summary>
    /// 从线程中获取数据访问上下文，保证线程内实例唯一
    /// </summary>
    public class ContextFactory
    {
        public static DbContext GetcurrentContext()
        {
            //从线程中找dbcontext
            DbContext context = CallContext.GetData("MemberPointModel") as DbContext;
            //如果获取不到
            if (context == null)
            {
                context = new MemberPointModel();//直接从MemberPointModel中取dbcontext
                CallContext.SetData("MemberPointModel", context);//存入线程中dbcontext
            }
            return context;
        }
    }
}

